<template>
  <el-button @click="exportPDFAndWord">
    <slot></slot>
  </el-button>
</template>
<script>

import {generatePDF,generateWord,getCoursesReportStatus} from "common/src/api/resource/course";
/**
 * 导出pdf，word组件
 */
export default {
  name: "ExportButton",
  model: {
    event: "click"
  },
  props: {
    queryForm: {
      type: Object
    },
    chooseCourses: {
      type: Array
    },
    exType:{
      type: Number
    }
  },
  data() {
    return {};
  },
  mounted() {},
  created() {},
  methods: {
    //================================================
    exportPDFAndWord(){
			let loading = this.$loading({
				lock: true,
				text: "Loading",
				spinner: "el-icon-loading",
				background: "rgba(255, 255, 255, .48)"
			});
			let data=_.cloneDeep(this.queryForm);
            data.idSet = this.chooseCourses.map(c => c.id);
			//facultys:支持多选所传参数,兼容之前数据类型,采用loadsh的 omit 方法移除原来college属性
			const params = _.omit(
				Object.assign(data, {
					facultys:data.college
				}),
				["college"]
      );
      if(params.facultys.length < 1){
				params.facultys = null;
			}
      if(this.exType == 1){
        	generatePDF(params).then(res => {
				if (res.code == 200) {
					this.delayRequestWordAndPDF(res.data.key, loading);
				}
			});
      }else{
        	generateWord(params).then(res => {
				if (res.code == 200) {
					this.delayRequestWordAndPDF(res.data.key, loading);
				}
			});
      }
		
    },
    async delayRequestWordAndPDF(key, loading) {
			await new Promise(resolve => setTimeout(resolve, 5000));
			getCoursesReportStatus(key)
				.then(res => {
					if (res.code == 200 && res.data.status) {
						loading.close();
						let path =
							"/api/cultureservice/courses/downLoadMore?path=" +
							res.data.path;
						window.location.href=path;
						//window.open(path);
					} else {
						this.delayRequestWordAndPDF(key, loading);
					}
				})
				.catch(err => {
					loading.close();
					throw err;
				});
		},
  }
};
</script>

<style scoped>
</style>
